package com.ntanh.mealplanner.base.model;

import android.content.ContentValues;
import android.util.Log;

import com.ntanh.mealplanner.main.objects.Ingredient;

import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public abstract class BaseEntity implements Identifiable, Activable, Timestampable, Undeletable {

    public static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    public static final String entitySplitSign = ";";

    public static final String ID = "id";

    public static final String CREATEDDATE = "createDate";

    public static final String MODIFIEDDATE = "modifyDate";

    public static final String CREATEDBY = "createdByy";

    public static final String MODIFIEDBY = "modifiedBy";

    public static final String ACTIVE = "active";

    public static final String DELETED = "deleted";

    public static final String SYNCED = "synced";

    protected Integer id;

    protected Date createdDate;

    protected Date modifiedDate;

    protected String createdBy;

    protected String modifiedBy;

    protected boolean active;

    protected boolean deleted;

    private boolean synced;

    public BaseEntity(String author) {
        this.createdDate = new Date();
        this.modifiedDate = createdDate;
        this.createdBy = author;
        this.modifiedBy = author;
        this.deleted = false;
        this.active = true;
        this.setSynced(false);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BaseEntity)) return false;

        BaseEntity that = (BaseEntity) o;

        if (!createdBy.equals(that.createdBy)) return false;
        if (!createdDate.equals(that.createdDate)) return false;
        if (!id.equals(that.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + createdDate.hashCode();
        result = 31 * result + createdBy.hashCode();
        return result;
    }

    @Override
    public Serializable getId() {
        return id;
    }

    @Override
    public void setId(Serializable id) {
        this.id = (Integer) id;
    }

    @Override
    public Date getCreatedDate() {
        return createdDate;
    }

    @Override
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Override
    public Date getModifiedDate() {
        return modifiedDate;
    }

    @Override
    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    @Override
    public String getCreatedBy() {
        return createdBy;
    }

    @Override
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Override
    public String getModifiedBy() {
        return modifiedBy;
    }

    @Override
    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    @Override
    public boolean isActive() {
        return active;
    }

    @Override
    public void setActive(boolean active) {
        this.active = active;
    }

    @Override
    public boolean isDeleted() {
        return deleted;
    }

    @Override
    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public boolean isSynced() {
        return synced;
    }

    public void setSynced(boolean synced) {
        this.synced = synced;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        return sb.toString();
    }

    public JSONObject toJSONObject() {
        JSONObject json = null;
        try {
            json = new JSONObject();
            json.put(BaseEntity.ID, id);
            json.put(BaseEntity.CREATEDDATE, createdDate);
            json.put(BaseEntity.MODIFIEDDATE, modifiedDate);
            json.put(BaseEntity.CREATEDBY, createdBy);
            json.put(BaseEntity.MODIFIEDBY, modifiedBy);
            json.put(BaseEntity.ACTIVE, active);
            json.put(BaseEntity.DELETED, deleted);
        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert BaseEntity to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        if (values == null) {
            values = new ContentValues();
        }
        /*if(!forInserting){
            values.put(ID, String.valueOf(getId()));
        }*/
        values.put(ACTIVE, isActive());
        values.put(CREATEDBY, getCreatedBy());
        values.put(MODIFIEDBY, getModifiedBy());
        values.put(DELETED, isDeleted());
        values.put(SYNCED, isSynced());
        values.put(CREATEDDATE, dateFormatter.format(getCreatedDate()));
        values.put(MODIFIEDDATE, dateFormatter.format(getModifiedDate()));
    }

    public abstract String getTableName();

}
